﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Unity;

namespace Infrastructure
{
    public abstract class ViewModelBase : INotifyPropertyChanged, IDataErrorInfo
    {
        public void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected IUnityContainer Container;
        protected IEventAggregator EventAggregator;

        public ViewModelBase()
        {

        }

        public ViewModelBase(IUnityContainer container, IEventAggregator eventAggregator)
        {
            this.Container = container;
            this.EventAggregator = eventAggregator;
        }

        string messageContent;

        public string MessageContent
        {
            get { return messageContent; }
            set
            {
                messageContent = value;
                OnPropertyChanged("MessageContent");
            }
        }

        bool isBusy;

        public bool IsBusy
        {
            get
            {
                return isBusy;
            }
            set
            {
                isBusy = value;
                OnPropertyChanged("IsBusy");
            }
        }


        public virtual void OnRemoved() { }
        public virtual void OnAdded() { }

        #region IDataErrorInfo Members

        private Dictionary<string, string> validationErrors = new Dictionary<string, string>();

        public void AddError(string propertyName, string message)
        {
            if (validationErrors.ContainsKey(propertyName) == false)
            {
                validationErrors.Add(propertyName, message);
                OnPropertyChanged(propertyName);
            }
        }

        public void RemoveError(string propertyName)
        {
            if (validationErrors.ContainsKey(propertyName) == true)
            {
                validationErrors.Remove(propertyName);
            }
        }

        public bool HasErrors
        {
            get { return validationErrors.Count > 0; }
        }

        public string Error
        {
            get
            {
                if (validationErrors.Count > 0)
                {
                    return "Data is invalid.";
                }
                else
                {
                    return null;
                }
            }
        }

        public string this[string propertyName]
        {
            get
            {
                if (validationErrors.ContainsKey(propertyName) == true)
                {
                    return validationErrors[propertyName];
                }
                return null;
            }
        }
        #endregion
    }
}
